/**
 * ----------------------------------------------------------------------------
 * Copyright Jens Konopka
 * 
 * Alle Rechte vorbehalten.
 * Licence: Apache 2.0
 * ----------------------------------------------------------------------------
 *
 * created on 05.03.2012 by jkonopka
 */
package de.sql.toolkit.generator;

import java.io.File;

import de.sql.toolkit.model.Column;
import de.sql.toolkit.model.Table;

/**
 * @author jkonopka
 * 
 */
public class JvModelFieldsGenerator extends AbstractJvModelGenerator {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1030548227891525459L;

	/**
	 * @param dstPath
	 * @param table
	 */
	public JvModelFieldsGenerator(File dstPath, Table table) {
		super(dstPath, table);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.sql.toolkit.writer.IWriter#write()
	 */
	@Override
	public String generate() throws Exception {
		final StringBuilder content = new StringBuilder();
		for (final Column column : getTable().getColumns()) {
			content.append(generateFieldRow(column));
		}
		content.append("\n");
		return content.toString();
	}

	/**
	 * @param column
	 * @return
	 */
	private String generateFieldRow(final Column column) {
		final StringBuilder content = new StringBuilder();
		content.append(generateFieldPre());
		content.append(generateFieldModifier());
		content.append(" ");
		content.append(generateFieldType(column));
		content.append(" ");
		content.append(generateFieldname(column));
		content.append(";\n");
		return content.toString();
	}
	
	/**
	 * @return
	 */
	protected String generateFieldPre() {
		return "\t";
	}

	/**
	 * @return
	 */
	private String generateFieldModifier() {
		return "private";
	}

	/**
	 * @param column
	 * @return
	 */
	private String generateFieldname(final Column column) {
		return prepareName(column.getName());
	}

	/**
	 * @param column
	 * @return
	 */
	private String generateFieldType(final Column column) {
		return getFieldType(column.getType(), column.getNullable());
	}

}
